import { isLogin, toLogin, getUserInfo, setUserInfo } from '../../utils/auth'
import area from '../../utils/area';
import { checkPhone } from "../../utils/validate";
import ValidateForm from "../../utils/validateForm";
import { showModel, showNotify, showToast, showDialog } from '../../utils/index'
import http from '../../utils/http'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,

        mobile: null,
        password: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({ isLogin: isLogin() })
    },

    /**
     * 登录
     */
    async handleLogin() {
        let { mobile, password } = this.data

        if(!checkPhone(mobile)) {
            return showToast({title: "手机号不正确"})
        }
        if(!password) {
            return showToast({title: "密码不能为空"})
        }

        let form = {mobile, password}

        let res = await http.post('/api/mall/umsmember/login', form)

        if(res.status) {
            // 将用户信息保存到缓存中
            setUserInfo(res.data)

            // 将用户信息保存到全局属性
            getApp().globalData.userInfo = res.data;

            wx.navigateBack({ delta: 1 });
        } else {
            showToast({title: res.msg})
        }
    },

})